package com.rslai.base.tool.servertest.paramfilter;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

public class TimestampParamFilter extends AbstractDateParamFilter {
    private static final Pattern pattern = Pattern.compile("DATE\\(\\s*([+-]?[0-9]+)\\s*,\\s*#([MmDdHhSs]+)\\s*,?\\s*([YyMDdHhmSs]?)\\s*\\)");

    private static final Map<String, Long> TIME_MAP = new HashMap();

    public TimestampParamFilter(Clock clock) {
        super(clock);
    }

    protected String postProcess(String param, String group1, String group2, String group3, String result) {
        return result;
    }

    protected String format(String param, String formatExpression, Date date) {
        Long integer = (Long) TIME_MAP.get(formatExpression.toLowerCase());
        if (integer != null) {
            return String.valueOf(date.getTime() / integer.longValue());
        }
        return String.valueOf(date.getTime());
    }

    protected Pattern getPattern() {
        return pattern;
    }

    static {
        TIME_MAP.put("ms", Long.valueOf(1L));
        TIME_MAP.put("s", Long.valueOf(TimeUnit.SECONDS.toMillis(1L)));
        TIME_MAP.put("m", Long.valueOf(TimeUnit.MINUTES.toMillis(1L)));
        TIME_MAP.put("h", Long.valueOf(TimeUnit.HOURS.toMillis(1L)));
        TIME_MAP.put("d", Long.valueOf(TimeUnit.DAYS.toMillis(1L)));
    }
}
